<?php
session_start();

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-11-19
 * Time: 上午9:29
 */
class StudentApi extends Api
{
    //获取体质报告
    /**
     * 此方法用于获取家长小孩的体质报告、
     * @param $stu_id 通过学生ID进行查询、
     * @return arr
     */
    public function get_report()
    {
        //学生ID
        $stu_id = $this->data['stu_id'];
        $report = D("health_report");
        $data = array('stu_id' => $stu_id);
        $arr = $report->where($data)->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array();
        }
    }
    /**
     *
     * 实名认证
     * 认证思路：通过传递的参数值与数据库进行匹配、若匹配成功则认证成功、否则认证失败
     * @param $cfc_id  学生id
     * @param $cic_name  幼儿园名称
     * @param $grad  年级
     * @param $cif_name  姓名
     * @param $cif_gender  性别
     * @param $cif_birthday  出生日期
     * @param $cif_class 班级
     * @return true or false
     */
    public function authentication()
    {
        $cfc_id = $this->data['cfc_id'];
        $cic_name = $this->data['cic_name'];
        $cif_grad = $this->data['cif_grad'];
        $cif_name = $this->data['cif_name'];
        $cif_gender = $this->data['cif_gender'];
        $cif_birthday = $this->data['cif_birthday'];
        $cif_class = $this->data['cif_class'];
        //准备条件
        $data = array('cfc_id' => $cfc_id, '
                        cic_name' => $cic_name, '
                        cif_grad' => $cif_grad, '
                        cif_class' => $cif_class, '
                        cif_birthday' => $cif_birthday, '
                        cif_name' => $cif_name, '
                        cif_gender' => $cif_gender
        );
        $m = D("certification");
        $arr = $m->where($data)->select();
        if (!empty($arr)) {//如果存在数据
            return true;
        } else {//否则
            return false;
        }
    }
    /**
     * 查询学生个人的报告列表
     *
     * 通过ID查询相对应学生报告信息
     * @param $stu_id 学生id
     * @return array or array()
     */
    public function get_report_of_person()
    {
        $stu_id = $this->data['stu_id'];
        $m = D("health_report");
        $arr = $m->where("stu_id = " . $stu_id)->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array();
        }
    }
    //获取最新报告
    public function get_new_report()
    {
        $stu_id = $this->data['stu_id'];
        $m = D("health_report");
        $arr = $m->where("stu_id = " . $stu_id)->limit(1)->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array();
        }
    }
    /**
     * 更换班级
     * @author
     * @param $stu_id  学生编号
     * @param $old_class  学生原班级
     * @param $now_class  学生当前班级
     * @return true or false
     */
    public function change_class()
    {
        $stu_id = $this->data['stu_id'];
        $old_class = $this->data['old_class'];
        $now_class = $this->data['now_class'];;
        //判断原班级与当前班级是否相同
        if ($old_class == $now_class) {
            return false;
        } else {//进行查询
            $m = D("student");
            $m->where(array('stu_id' => $stu_id, '`class`' => $old_class))->select();
            //判断该学生信息是否存在
            if (!empty($m)) {//如果存在
                //执行更新操作
                $data = array('class' => $now_class);
                return $m->where(array('stu_id' => $stu_id))->save($data);
            } else {//不存在则返回false
                // return false;
                return array('status' => 0, 'message' => '不存在数据');
            }
        }
    }
    //1，数据录入
    /**
     * 添加测试数据
     * @param $grad 年级
     * @param $class 班级
     * @param $name 姓名
     * @param $year 测试年份
     * @param $quarter 测试季度
     * @param $time 测试时间
     * @param $balanceBeam 走平衡木
     * @param $continuousJump 双脚连续跳
     * @param $flexion 立体前屈
     * @param $standing_long_jump 立定跳远
     * @param $vision_right  右眼视力
     * @param $tennis_throw  网球投掷
     * @param $reentry_run   折返跑
     * @param $vision_left  左眼视力
     * @param $skinfold_shoulder_armor 肩甲皮褶
     * @param $skinfold_abdomen 腹部皮褶
     * @param $heart_rate 心率
     * @param $skinfold_upper_arm 上臂皮褶
     * @param $weight  体重
     * @param $sit_high  坐高
     * @param $modes_transportation 来园方式
     * @param $height 身高
     * @return true or false
     */
    public function add_test_data()
    {
        $grad = $this->data['grad'];
        $class = $this->data['class'];
        $name = $this->data['name'];
        $year = $this->data['year'];
        $quarter = $this->data['quarter'];
        $time = $this->data['time'];
        $balanceBeam = $this->data['balanceBeam'];
        $continuousJump = $this->data['continuousJump'];
        $flexion = $this->data['flexion'];
        $standing_long_jump = $this->data['standing_long_jump'];
        $vision_left = $this->data['vision_left'];
        $tennis_throw = $this->data['tennis_throw'];
        $reentry_run = $this->data['reentry_run'];
        $vision_right = $this->data['vision_right'];
        $skinfold_shoulder_armor = $this->data['skinfold_shoulder_armor'];
        $skinfold_abdomen = $this->data['skinfold_abdomen'];
        $heart_rate = $this->data['heart_rate'];
        $skinfold_upper_arm = $this->data['skinfold_upper_arm'];
        $weight = $this->data['weight'];
        $sit_high = $this->data['sit_high'];
        $modes_transportation = $this->data['modes_transportation'];
        $height = $this->data['$height'];
        //准备数据
        $data = array(
            'grad' => $grad,
            'class' => $class,
            'name' => $name,
            'test_year' => $year,
            'quarter' => $quarter,
            'test_time' => $time,
            'balanceBeam' => $balanceBeam,
            'continuousJump' => $continuousJump,
            'flexion' => $flexion,
            'standing_long_jump' => $standing_long_jump,
            'vision_right' => $vision_right,
            'tennis_throw' => $tennis_throw,
            'reentry_run' => $reentry_run,
            'vision_left' => $vision_left,
            'skinfold_shoulder_armor' => $skinfold_shoulder_armor,
            'skinfold_abdomen' => $skinfold_abdomen,
            'heart_rate' => $heart_rate,
            'skinfold_upper_arm' => $skinfold_upper_arm,
            'weight' => $weight,
            'sit_high' => $sit_high,
            'modes_transportation' => $modes_transportation,
            'height' => $height
        );
        $m = D("test_data");
        $b = $m->add($data);
        if ($b) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 获取班级学生列表
     * 通过班级编号进行查询对应班级的学生
     * @param $class_id 班级编号
     * @return array()
     */
//    public function get_student_list()
//    {
//        $class_id = $this->data['class_id'];
//        $m = D("student");
//        $arr = $m->where(array('class' => $class_id))->select();
//        if (!empty($arr)) {
//            print_r($arr);
//        } else {
//            return array();
//        }
//    }

    /**
     * 已测试学生列表(已收到报告)
     * @return array()
     */
    public function get_is_test()
    {
        $is_test = 1;//表示已测
        $m = D("test_data");
        $arr = $m->where(array('is_test' => $is_test))->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array("status" => 0, 'message' => '没有找到相关数据');
        }
    }
    /**
     * 未测试学生列表(未收到报告)
     * @return array()
     */
    public function get_not_test()
    {
        $is_test = 0;//表示未测试
        $m = D("test_data");
        $arr = $m->where(array('is_test' => $is_test))->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array();
        }
    }
    //获取未测试的数据列表
    //获取未测数据列表的思路:1.查询已测列表中的学生ID、2.查询学生列表中不存在于已测学生列表中的ID。
    //3.根据不存在与已测学生列表中的ID查询出相关学生信息。内容包含有（测试的项目名、传回数据名、图片的URL）
    //参数方面:1.可根据用户传入的参数决定查询的是市级未测、区级未测、园级未测以及班级未测。
    //默认查询的是市级未测.
    //故此接口首先判断调用此接口的用户是市级、区级、园级还是班级权限。
    /**
     * 获取未测试学生列表
     *
     * 根据不同UID查找其对应的权限、根据需要获得未测试学生列表
     * @return string
     */
    public function not_tested()
    {
        $uid = $this->data['uid'];
        $arr = D("users")->field("admin_level")->where(array('uid' => $uid))->find();
        //当前用户ID的管理权限
        $level = intval($arr['admin_level']);
        if ($level == 4) {//班级管理权限
            //得到条件数组
            $arr = self::getArea($uid);
            //得到对应班级下的已测学生ID
            $stu_id = D("test_data")->
            field("stu_id")->
            where(array('school_name' => $arr[1], '
                                 area' => $arr[2], '
                                 city' => $arr[3], '
                                 class_name' => $arr[0]))
                ->select();
            //将已测学生ID二维数组加工为一维数组
            $id = array_column($stu_id, 'stu_id');
            //加入条件
            $map['stu_id'] = array('not in', $id);
            $map['school_name'] = $arr[1];
            $map['area'] = $arr[2];
            $map['city'] = $arr[3];
            $map['class_name'] = $arr[0];
            $stu = D("student")->where($map)->select();
            //返回结果集
            echo "<pre/>";
        } elseif ($level == 3) {//园级管理权限
            //得到数组条件
            $arr = self::getArea($uid);
            //得到已测试学生ID
            $stu_id = D("test_data")->
            field("stu_id")->
            where(array('school_name' => $arr[0], '
                                 area' => $arr[1], '
                                 city' => $arr[2]))
                ->select();
            //得到一维的已测学生ID数组
            $id = array_column($stu_id, 'stu_id');
            //加入条件
            $map['stu_id'] = array('not in', $id);
            $map['school_name'] = $arr[0];
            $map['area'] = $arr[1];
            $map['city'] = $arr[2];
            //再到学生列表中进行查询
            $students = D("student")->where($map)->select();
            return $students;
        } elseif ($level == 2) {//区级管理权限
            return "待开发中，敬请关注";
        } elseif ($level == 1) {//市级管理权限
            return "待开发中，敬请关注";
        }
    }
    //编写一个方法用于获取其父级管理区域、如果其父级管理权限不等于1、便一直查找。
    //当父级管理权限等于1则退出递归调用。
    //返回一个数组
//    public function get_father_name($uid = 5){
//        //准备数据:
//        $father_name = array();
//        //1.查询父ID
//        $m = D("users");
//        $arr = $m->field("father_id")->where(array('uid'=>$uid))->select();
//        $fid = $arr[0]['father_id'];
//        //2.根据其父ID查询其相应的管理权限
//        $level = $m->field("admin_level")->where(array('uid'=>$fid))->find();
//        $le = $level['admin_level'];
//        //3.查询出本身的所在管理区域
//        $rs = $m->field("area")->where(array('uid'=>$uid))->find();
//        if($level != 0 && $rs != '北京市'){//如果父类权限不等于0那么将其父名称添加到$father_name数组中
//           $father_name = array();
//            //查询出父管理区域
//           $area = $m->field("area")->where(array('admin_level'=>$le,'uid'=>$fid))->find();
//           $ar = $area['area'];
//           array_push($father_name,$ar);
//           self::get_father_name($fid);
//        }else{
//            print_r($father_name);
//        }
//    }
    /**
     * 获取用户ID的父区域
     *
     * @param int $uid 用户ID
     * @param array $data 封装数据的数组
     * @return array
     */
    public function getArea($uid, array $data = array())
    {
        $uid = intval($uid);
        $temp = D('Users')->where(array('uid' => $uid))->find();
        if ($temp) {
            array_push($data, $temp['area']);
            if ($temp['father_id'] > 1) {
                return $this->getArea($temp['father_id'], $data);
            }
        }
        return $data;
    }
//递归调用
//    public function get_father_nm($uid=5){
//        $data = D('users')->where(array('uid' => intval($uid)))->find();
//        echo '<pre>';
//        print_r($data);
//        exit;
//        $father_name = array();
//        $m = D("users");
//        //查询本身的管理权限
//        $arr = $m->field("admin_level")->where(array('uid'=>$uid))->find();
//        //得到本身的管理权限
//        $level = $arr['admin_level'];
//        //得到本身的管理区域
//        $arr2 = $m->field("area")->where(array('uid'=>$uid))->find();
//        $area = $arr2['area'];
//        array_push($father_name,$area);
//        //得到父ID
//        $arr3 = $m->field("father_id")->where(array('uid'=>$uid))->select();
//        $fid = $arr3[0]['father_id'];
//        //得到父管理权限
//        $level2 = $m->field("admin_level")->where(array('uid'=>$fid))->find();
//        //$fle = $level2['admin_level'];
//        if($level != 1){
//            self::get_father_nm($fid);
//        }
////        return $father_name;
//        print_r($father_name);

//    public function get_father_nms($uid=5){
//        //准备一个数组用于封装返回的结果集
//        $father_name = array();
//        while(true){
//            $m = D("users");
//            $arr = $m->field("admin_level")->where(array('uid'=>$uid))->find();
//            //得到本身的管理权限
//            $level = $arr['admin_level'];
//            $arr2 = $m->field("area")->where(array('uid'=>$uid))->find();
//            //得到本身的管理区域
//            $area = $arr2['area'];
//            $level2 = $m->field("admin_level")->where(array('uid'=>$fid))->find();
//            $fle = $level2['admin_level'];
//            //判断其本身的管理权限及父管理权限
//            if($level !=1 && $fle !=1){
//                array_push($father_name,$area);
//            }else{
//                return;
//            }
//        }
//        print_r($father_name);
//    }

//=============================健康平台接口================================
    /**
     * @return array
     */
    public function login()
    {
        $type = $_REQUEST['type'] ? $_REQUEST['type'] : 1;//1健康 2图书
        if(!empty($_REQUEST['uname']) && !empty($_REQUEST['pwd'])){
            $username = addslashes($_REQUEST['uname']);
            $password = addslashes($_REQUEST['pwd']);
            $map = "(phone = '{$username}' or uname='{$username}' or email='{$username}') AND is_del=0 AND no_login=1 and permissions=".$type;
            //根据帐号获取用户信息
            $user = model('User')->where($map)->field('uid,password,uname,login_salt,is_audit,is_active,fid')->find();
            //判断用户名密码是否正确
            if($user && md5(md5($password).$user['login_salt']) == $user['password']){
                if(model('DisableUser')->isDisableUser($user['uid'])){
                    return array('status'=>0,'msg'=>'您的帐号被已管理员禁用');
                }
                //如果未激活提示未激活
                if($user['is_audit']!=1){
                    return array('status'=>0,'msg'=>'您的帐号尚未通过审核');
                }
                if($user['is_active']!=1){
                    return array('status'=>0,'msg'=>'您的帐号尚未激活,请进入邮箱激活');
                }

                //获取用户组
                $group_id = M('user_group_link')->where('uid='.$user['uid'])->getField('user_group_id');
                if($group_id == 4 || $group_id == 5) {
                    if( $type == 1) {//健康平台账号
                        if($group_id == 4) {
                            //如果是园级账号 则获取班级列表
                            $school_id = M('school')->where('fid='.$user['uid'])->getField('id');
                            $user['class_list'] = M('class')->where('is_del=0 and school_id='.$school_id)->findAll();
                            foreach ($user['class_list'] as &$value) {
                                if( $value['is_up'] == 1 ) {
                                    if($value['class_name1']) {
                                        $value['class_name'] = $value['class_name1'];
                                    } else {
                                        $value['class_name'] = $value['class_name'];
                                    }
                                } 

                                if( $value['is_up'] == 2 ){
                                    if($value['class_name2']) {
                                        $value['class_name'] = $value['class_name2'];
                                    } else {
                                        $value['class_name'] = $value['class_name1'];
                                    }
                                }
                            }
			    $user['message']    = 1;
                        } else {
                            $user['class_list'] = M('class')->where('admin_id='.$user['uid'])->find();
                            if( $user['class_list']['is_up'] == 1 ) {
                                if($user['class_list']['class_name1']) {
                                    $user['class_list']['class_name'] = $user['class_list']['class_name1'];
                                } else {
                                    $user['class_list']['class_name'] = $user['class_list']['class_name'];
                                }
                            } 

                            if( $user['class_list']['is_up'] == 2 ){
                                if($user['class_list']['class_name2']) {
                                    $user['class_list']['class_name'] = $user['class_list']['class_name2'];
                                } else {
                                    $user['class_list']['class_name'] = $user['class_list']['class_name1'];
                                }
                            }
                            $user['message']    = 0;
                        }
                    } else {//图书账号
                        if($user['fid'] == 0) {
                            $user['message']    = 1;//园级账号
                        } else {
                            $user['message']    = 0;//班级/办公室账号
                        }
                    }
                    $user['user_group'] = $group_id;
                    $user['status'] = 1;
                    //清理预订时间失效的书籍
                     $book_reserve=M("book_reserve_box")->where("del=0 and reserve_time")->select();
                     if($book_reserve){
                        foreach ($book_reserve as $key => $value) {
                            if(strtotime($value["reserve_time"])<time()){
                                M("book_reserve_box")->where("id=".$value['id'])->save(array('del'=>3));
                             }
                         }
                     }
                    return $user;
                
                } else {
                    return array('status'=>0,'msg'=>'你不是园级或班级账号');
                }
                
            }else{
                return array('status'=>0,'msg'=>'用户名或密码错误');
            }
        }else{
            return array('status'=>0,'msg'=>'用户名或密码不能为空');
        }

    }
    
    //教师获取班级列表
    public function getClassList(){
        $uid = $this->data['uid'];
        $map['class_id'] = M('users')->where('uid='.$uid)->getField('class');
        $list = M('class')->where($map)->order('ctime desc')->limit($this->_limit())->findAll();
        return $list ? $list : array();
    }
    
//    public function login()
//    {
//        $phone = $this->data['phone'];
//        $pwd = $this->data['pwd'];
//        $users = D("users");
//        $data['phone'] = $phone;
//        $data['pwd'] = $pwd;
//        $user = $users->where($data)->select();
//        if (!empty($user)) {
//            //登录成功
//            session("Oauth_token",$phone);
//            session("Oauth_token_secret",$pwd);
//            return array(
//                'status' => 1,
//                'msg' => '登录成功'
//            );
//        } else {
//            return array(
//                'status' => 0,
//                'msg' =>$user->getMessage()
//            );
//        }
//    }
    /**
     * 游客注册接口
     *
     * @param $user
     * @param $pwd
     * @param int $level
     * @return bool
     */
    public function add_user_info($level = 5)
    {
        $user = $this->data['userName'];
        $pwd = $this->data['pwd'];
        $phone = $this->data['phone'];
        $arr = D("users")->where(array('phone' => $phone))->select();
        if (!empty($arr)) {
            return array(
                'status' => 0,
                'msg' => '该用户已注册、不能进行注册'
            );
        } else {
            $data['uname'] = $user;
            $data['pwd'] = $pwd;
            $data['admin_level'] = $level;
            $data['phone'] = $phone;
            $bool = D("users")->add($data);
            if ($bool) {
                return array(
                    'status' => 1,
                    'msg' => '注册成功'
                );
            } else {
                return array(
                    'status' => -1,
                    'msg' => $bool->getMessage()
                );
            }
        }
    }
    /**
     * 找回密码
     * @request phone 用户手机号
     * @request newPwd 新密码
     * @request confirmPwd 重复密码
     * @return array 状态值+msg
     */
    public function  retrieve_password()
    {
        //用户手机号码
        $phone = $_REQUEST['phone'];
        //新密码
        $newPwd = $_REQUEST['newPwd'];
        //确认密码
        $confirmPwd = $_REQUEST['confirmPwd'];
        //验证该手机号码是否存在
        $data['phone'] = $phone;
        $arr = D("users")->where($data)->select();
        if (!empty($arr)) {//验证成功,允许用户输入密码
            //新密码不能为空
            if ($newPwd == array() || $confirmPwd == array() || trim($newPwd) == '' || trim($confirmPwd) == '') {
                return array('status' => -1, 'message' => '新密码不能为空');
                //密码不相等
            } elseif ($confirmPwd != $newPwd) {
                return array('status' => -2, 'message' => '密码不一致');
            } else {
                //修改密码
                $data['phone'] = $phone;
                $pwd['pwd'] = $newPwd;
                D("users")->where($data)->save($pwd);
                return array('status' => 1, 'msg' => '修改成功');
            }
        } else {
//            $aee = array('static' => 0, 'message' => 'gehe');
//            echo json_encode($aee);exit;
            return array('status' => 0, 'message' => '验证失败:该用户不存在');
        }
    }
    //
    /**
     * 获取无限回复的消息列表
     * @return array 状态+msg
     */
    public function get_information()
    {
        $data['pid'] = 0;
        $arr = D("infinite_replay")->where($data)->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array('status' => 0, 'message' => '数据为空');
        }
    }
    /**
     * 用于获取用户的手机型号
     * 获取消息来源
     * @return string
     */
    public function get_phone_verson()
    {
        $from = $_REQUEST['from'];
        if (!empty($from)) {
            return array('status' => 1, 'msg' => '返回成功');
        } else {
            return array('status' => 0, 'msg' => '返回失败');
        }
    }
    //通过用户ID获取用户的头像信息
    public function get_img_url()
    {
        ob_end_clean();
        ob_start();
        header('content-type:image/jpg');
        echo file_get_contents(THEME_PUBLIC_URL . '/image/health/01.jpg');
        ob_end_flush();
        exit;
    }
    //通过用户ID获取用户的名称
    public function get_user_name()
    {
        $uid = $this->data['uid'];
        $data['uid'] = $uid;
        $arr = D("users")->field('uname')->where($data)->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array('status' => 0, 'message' => '没有数据');
        }
    }
    //显示评论数
    public function show_comment_num()
    {
        $ift_id = intval($this->data['ift_id']);
        $data['pid'] = $ift_id;
        $num = D("infinite_replay")->field("count(*) as num")->where($data)->select();
        if (!empty($num)) {
            return array_column($num, "num");
        } else {
            return array('num' => 0);
        }
    }
    /**
     *  添加评论
     * ift_id 评论对象的发帖ID
     * comment 评论内容
     * from 手机型号
     * uid 发表评论者的用户ID
     * @return array 状态值+msg
     */
    public function add_comment_content()
    {
        //获取数据
        $ift_id = $this->data['ift_id'];
        $comment = $this->data['comment'];
        $phone_type = $this->data['from'];//手机类型
        $uid = $this->data['uid'];
        //准备数据
        $data['pid'] = $ift_id;//pid
        $data['ift_content'] = $comment;//评论内容
        $data['from_type'] = $phone_type;//手机类型
        $data['uid'] = $uid;
        $data['release_time'] = date('Y-m-d');
        //添加评论数据
        $bool = D("infinite_replay")->add($data);
        if ($bool > 0) {
            return array('status' => 1, 'message' => '发表成功');
        } else {
            return array('status' => 0, 'message' => '发表失败');
        }
    }
    /**
     * 获取评论列表
     * @return array 状态+msg
     */
    public function get_comment_list()
    {
        $ift_id = $this->data['ift_id'];
        $data['pid'] = $ift_id;
        $arr = D("infinite_replay")->where($data)->order("release_time desc")->limit(10)->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array(
                'status' => 0,
                'message' => '暂时没有评论'
            );
        }
    }
    /**
     * 获取详细评论列表
     * @return array 状态+msg
     */
    public function get_description_comment_list()
    {
        $ift_id = $this->data['ift_id'];
        $data['pid'] = $ift_id;
        $arr = D("infinite_replay")->where($data)->order("release_time desc")->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array(
                'status' => 0,
                'message' => '暂时没有评论'
            );
        }
    }
    //点赞或者取消点赞
    public function click_or_cancel_praise()
    {
        $bool = $_REQUEST['bool'];
        $data['ift_id'] = $_REQUEST['ift_id'];
        $num = D("infinite_replay")->field("praise_num")->where($data)->select();
        $praise_num = array_column($num, "praise_num");
        //获得当前评论的点赞数
        $number = $praise_num[0];
        if ($bool == "true") {
            $save['praise_num'] = $number + 1;
            D("infinite_replay")->where($data)->save($save);
            return array(
                'status' => 1,
                'message' => '点赞成功',
                'isChange' => 'YES'
            );
        } else {
            $save['praise_num'] = $number - 1;
            D("infinite_replay")->where($data)->save($save);
            return array(
                'status' => 0,
                'message' => '已取消点赞',
                'isChange' => 'NO'
            );
        }
    }
    /**
     * 注册帐号 --using
     * @param varchar phone 手机号
     * @param varchar regCode 验证码
     * @param varchar uname 用户名
     * @param varchar password 密码
     * @param integer sex 性别 1-男 2-女
     * @param varchar avatar_url 头像地址
     * @param integer avatar_width 头像宽度
     * @param integer avatar_height 头像高度
     * @return array 状态值+提示信息
     */
    public function register()
    {
//        echo "hello";
        $regmodel = model('Register');
        $registerConfig = model('Xdata')->get('admin_Config:register');
        $phone = t($_POST['phone']);
        $regCode = t($_POST['regCode']);
        $uname = t($_POST['uname']);
        $sex = intval($_POST['sex']);
        $password = t($_POST['password']);
        //return array('status'=>0, 'msg'=>'注册失败，必须设置头像');
//        if(in_array('face', $registerConfig['personal_required']) && $_POST['avatar_url'] == ''){
//            return array('status'=>0, 'msg'=>'注册失败，请上传头像');
//        }
//        $avatar['picurl'] = $_POST['avatar_url'];
//        $avatar['picwidth'] = intval($_POST['avatar_width']);
//        $avatar['picheight'] = intval($_POST['avatar_height']);
        // //手机号验证
        // if ( !model('Sms')->checkRegisterCode( $phone , $regCode ) ){
        //  $return = array('status'=>0, 'msg'=>'验证码错误');
        // }

        /* # 验证手机号码 */
        if (($sms = model('Sms')) and !$sms->CheckCaptcha($phone, $regCode)) {
            return array(
                'status' => 0,
                'msg' => $sms->getMessage()
            );
        }
        unset($sms);

        if (!$regmodel->isValidPhone($phone)) {
            $msg = $regmodel->getLastError();
            $return = array('status' => 0, 'msg' => $msg);
            return $return;
        }
        /*
        //头像验证
        if($avatar['picurl'] && $avatar['picwidth'] && $avatar['picheight']){
            //code
        }else{
            $required = $this->registerConfig['personal_required'];
            if(in_array('face', $required)) return array('status'=>0, 'msg'=>'请上传头像');
        }*/
        //用户名验证
        if (!$regmodel->isValidName($uname)) {
            $msg = $regmodel->getLastError();
            $return = array('status' => 0, 'msg' => $msg);
            return $return;
        }
        //密码验证
        if (!$regmodel->isValidPasswordNoRepeat($password)) {
            $msg = $regmodel->getLastError();
            $return = array('status' => 0, 'msg' => $msg);
            return $return;
        }
        //开始注册
        $login_salt = rand(11111, 99999);
        $map['uname'] = $uname;
        $map['sex'] = $sex;
        $map['login_salt'] = $login_salt;
        $map['password'] = md5(md5($password) . $login_salt);
        $map['phone'] = $_POST['login'] = $phone;
        $map['ctime'] = time();
        $map['is_audit'] = $registerConfig['register_audit'] ? 0 : 1;
        // $map['is_audit'] = 1;
        $map['is_active'] = 1; //手机端不需要激活
        $map['is_init'] = 1; //手机端不需要初始化步骤
        $map['first_letter'] = getFirstLetter($uname);
        if (preg_match('/[\x7f-\xff]+/', $map['uname'])) {    //如果包含中文将中文翻译成拼音
            $map['search_key'] = $map['uname'] . ' ' . model('PinYin')->Pinyin($map['uname']);
        } else {
            $map['search_key'] = $map['uname'];
        }
        $uid = model('User')->add($map);
        if ($uid) {
            //第三方登录数据写入








            if (isset($this->data['type'])) {
                $other['oauth_token'] = addslashes($this->data['access_token']);
                $other['oauth_token_secret'] = addslashes($this->data['refresh_token']);
                $other['type'] = addslashes($this->data['type']);
                $other['type_uid'] = addslashes($this->data['type_uid']);
                $other['uid'] = $uid;
                M('login')->add($other);
            }
            // 添加至默认的用户组
            $userGroup = empty($registerConfig['default_user_group']) ? C('DEFAULT_GROUP_ID') : $registerConfig['default_user_group'];
            model('UserGroupLink')->domoveUsergroup($uid, implode(',', $userGroup));
            // 添加双向关注用户
            $eachFollow = $registerConfig['each_follow'];
            if (!empty($eachFollow)) {
                model('Follow')->eachDoFollow($uid, $eachFollow);
            }
            // 添加默认关注用户
            $defaultFollow = $registerConfig['default_follow'];
            $defaultFollow = array_diff(explode(',', $defaultFollow), explode(',', $eachFollow));
            if (!empty($defaultFollow)) {
                model('Follow')->bulkDoFollow($uid, $defaultFollow);
            }
            //保存头像
//            if($avatar['picurl'] && $avatar['picwidth'] && $avatar['picheight']){
//                $dAvatar = model('Avatar');
//                $dAvatar->init($uid);
//                $data['picurl'] = $avatar['picurl'];
//                $data['picwidth'] = $avatar['picwidth'];
//                $scaling = 5;
//                $data['w'] = $avatar['picwidth'] * $scaling;
//                $data['h'] = $avatar['picheight'] * $scaling;
//                $data['x1'] = $data['y1'] = 0;
//                $data['x2'] = $data['w'];
//                $data['y2'] = $data['h'];
//                $dAvatar->dosave($data, true);
//            }
            if ($map['is_audit'] == 1) {
                return $this->authorize();
//              $return = array('status'=>1, 'msg'=>'注册成功', 'need_audit'=>0);
            } else {
                $return = array('status' => 1, 'msg' => '注册成功，请等待审核', 'need_audit' => 1);
            }

            return $return;
        } else {
            $return = array('status' => 0, 'msg' => '注册失败');
            return $return;
        }
    }
    //显示点赞数
    public function  show_praise_num()
    {
        $data = $this->data['ift_id'];
        D("infinite_replay")->where()->select();
    }
    /**
     * 显示学生列表
     * @return array
     */
    public function get_student_list()
    {
        $area = $this->data['area'];
        //得到条件数组
        $location = preg_split('/\s+/', $area);

        $data['city'] = $location[0];
        $data['area'] = $location[1];
        $data['school_name'] = $location[2];
        $data['class_name'] = $location[3];

        //执行查询获取学生姓名、ID列表

        $stu_name = D("student")->field("stu_id,stu_name")->where($data)->select();

        if (!empty($stu_name)) {
            return array(
                'status' => 1,
                'student_info' => $stu_name
            );
            // return $stu_name;
        } else {
            return array(
                'status' => 0,
                'message' => '暂无学生数据、请检查用户所在区域信息'
            );
        }
    }
    /*获取已发送报告列表*/
    public function get_is_push()
    {
        $uid = $_REQUEST['uid'];
        $area = $this->getArea($uid);
        print_r($area);
    }
    

    /*获取当前教师用户的学生列表*/
    public function  get_student_name()
    {
        //查询当前用户的班级ID
        $where['class_id'] = $this->data['id'];
        $arr = D("student")->field("stu_id,stu_name")->where($where)->select();
        for ($i = 0; $i < count($arr); $i++) {
            if ($this->is_complete_report($arr[$i]['stu_id'])) {

            }
        }

        if (!empty($arr)) {
            return array(
                'status' => 1,
                'data' => $arr,
                'message' => '成功返回学生信息'
            );
        }
        return array(
            'status' => 0,
            'data' => array(),
            'message' => '哎呀、好像没有数据'
        );

    }
    /*判断是否完成项目测试*/
    public function is_complete_report($stu_id)
    {
        $data['stu_id'] = $stu_id;
        $data['balanceBeam'] = array('neq', '');
        $data['continuousJump'] = array('neq', '');
        $data['flexion'] = array('neq', '');
        $data['standing_long_jump'] = array('neq', '');
        $data['vision_right'] = array('neq', '');
        $data['tennis_throw'] = array('neq', '');
        $data['reentry_run'] = array('neq', '');
        $data['vision_left'] = array('neq', '');
        $data['skinfold_abdomen'] = array('neq', '');
        $data['skinfold_shoulder_armor'] = array('neq', '');
        $data['heart_rate'] = array('neq', '');
        $data['skinfold_upper_arm'] = array('neq', '');
        $data['weight'] = array('neq', '');
        $data['sit_high'] = array('neq', '');
        $data['modes_transportation'] = array('neq', '');
        $data['height'] = array('neq', '');
        $data['is_del'] = 0;
        $arr = D("test_data")->where($data)->find();
        if (!empty($arr)) {
            return true;
        }
        return false;
    }
    /*获取已发送报告列表*/
    public function get_was_push_report()
    {
        $uid = $_REQUEST['uid'];
        $data['class_id'] = $this->get_class_id($uid);
        $data['is_push'] = 1;
        $result = D("health_report")->field("rpt_id,rpt_name,push_time")->where($data)->select();
        if (!empty($result)) {
            return array(
                'status' => 1,
                'data' => $result,
                'message' => '已获取到报告'
            );
        }
        return array(
            'status' => 0,
            'data' => array(),
            'message' => '未获取到报告'
        );
    }
    /*发送报告*/
    public function push_report($stu_id)
    {
        $stu = D("test_data")
            ->where(array('is_del'=>0,'stu_id' => $stu_id))
            ->select();
        /*#表示该学生已做过测试项目#*/
        if (!empty($stu)) {
            $data['stu_id'] = $stu_id;
            $data['send_time'] = date('Y-m-d h:i:s');
            $bool = D("send_out")->add($data);
            if ($bool > 0) {
                return array(
                    'status' => '1',
                    'stu_id' => $stu_id,
                    'message' => '发送成功'
                );
            }
            return array(
                'status' => 0,
                'message' => '未知的错误'
            );
        } else {
            return array(
                'status' => 0,
                'stu_id' => $stu_id,
                'message' => '发送失败:该生未作测试项目'
            );
        }
    }
    /*#发送多个学生报告  一键推送#*/
    public function send_report_two()
    {
        $map['class_id'] = $this->data['id'];
        $ids = M('student')->where($map)->field('stu_id')->findAll();
        $ids = getSubByKey($ids , 'stu_id');
        /*发送成功的学生ID*/
        $success = array();
        /*发送失败的学生ID*/
        $fail = array();

        /*发送成功的学生姓名*/
        $success_name = array();
        /*发送成功的学生姓名*/
        $fail_name = array();

        for ($i = 0; $i < count($ids); $i++) {
            $arr = $this->push_report($ids[$i]);
            if ($arr['status'] == "1") {
                array_push($success, $ids[$i]);
            } else {
                array_push($fail, $ids[$i]);

            }
        }
        for ($i = 0; $i < count($success); $i++) {
            array_push($success_name,
                $this->return_name($success[$i]));
        }

        for ($i = 0; $i < count($fail); $i++) {
            array_push(
                $fail_name,
                $this->return_name($fail[$i])
            );
        }

        if (count($success_name) == 0 && count($fail_name) == 0) {
            return array(
                'status' => 0,
                'success' => array(),
                'fail' => array(),
                'message' => '请联系管理员'
            );
        } else {
            return array(
                'status' => 1,
                'success' => $success_name,
                'fail' => $fail_name,
                'message' => '已返回相关数据'
            );
        }

    }
    /*根据学生ID返回学生的姓名*/
    public function return_name($stu_id)
    {
        $stu_name = D("student")
            ->field("stu_name")
            ->where(array('stu_id' => $stu_id))
            ->find();

        return $stu_name['stu_name'];
    }
    /*获取报告详情*/
    public function get_detailed_report()
    {
        $data['test_id'] = $_REQUEST['rpt_id'];
        $data['is_del'] = 0;
        $res = M('test_data')->where($data)->find();
        if ($res) {
            $res['student_info'] = M('student')->where('stu_id='.$res['stu_id'])->find();
            $res['student_info']['school_name'] = M('school')->where('id='.$res['student_info']['school_id'])->getField('name');
            $res['student_info']['class_name'] = M('class')->where('class_id='.$res['student_info']['class_id'])->getField('class_name');
            if( $res['status'] == 1 ) {
                $res['comment'] = M('health_report')->where('stu_id='.$res['stu_id'])->getField('comment');
            }
            $res['english_china_name'] = M('test_project')->getField('english_name,project_name');
            $res['english_unit_name']  = M('test_project')->getField('english_name,unit');
            $res['english_name'] = getSubByKey(M('test_project')->field('english_name')->findAll() , 'english_name');
        }
        if ( $res ) {
            return array(
                'status' => 1,
                'data' => $res,
                'message' => '已获取到详细信息报告'
            );
        }
        return array(
            'status' => 0,
            'data' => array(),
            'message' => '抱歉、数据好像丢失了'
        );
    }
    /*通过用户ID返回用户的班级编号*/
    public function get_class_id($uid)
    {
        $result = D("users")->field("class_id")
            ->where(array('uid' => $uid))
            ->find();

        if (!empty($result)) {
            return $result['class_id'];
        }
        return array();

    }
    /*获取健康测试列表*/
    public function get_test_list()
    {
        $project = D("test_project")
            ->field('project_id,project_name,english_name,project_type')
            ->select();
        foreach($project as &$val) {
            if($val['project_type'] == 0 || $val['project_type'] == 3 || $val['project_type'] == 4 ) {
                $val['project_type'] = 0;
            }
            if($val['project_type'] == 1) {
                $val['project_type_txt'] = '身体形态';
            }else if($val['project_type'] == 2) {
                $val['project_type_txt'] = '身体素质';
            } else {
                $val['project_type_txt'] = '身体机能';
            }
        }
        if (!empty($project)) {
            return array(
                'status' => 1,
                'data' => $project,
                'message' => '成功返回项目信息'
            );
        }
        return array(
            'status' => 0,
            'data' => array(),
            'message' => '返回失败'
        );

    }
    /*封装单人录入测试数据*/
    public function logging_data($test_ids,$projects,$score,$stu_id,$name,$is_report,$test_number,$year){
        $test_id = $test_ids;//测试编号
        $data[$projects] = $score;
        $data['stu_id'] = $stu_id;
        $data['name'] = $name;
        $data['class_id'] = M('student')->where('stu_id='.$stu_id)->getField('class_id');
        $class = M('class')->where('class_id='.intval($data['class_id']))->find();
        $data['class_type'] = $class['class_type'];
        $data['year'] = $year;
        $data['date'] = date('Y-m-d');
        $data['ctime'] = time();
        $data['is_report'] = $is_report;
        $data['test_number'] = $test_number;
        $data['admin_id'] = $this->mid;
        $res = M('test_data')->where('stu_id='.$stu_id.' and is_report='.$is_report.' and test_number='.$test_number.' and is_del=0 and year='.$year)->getField('test_id');
        if( !$res ){//表示是新增录入数据
              $bool = D("test_data")->add($data);
              if($bool>0){
                    return array(
                        'status'=>1,
                        'message'=>"录入成功"
                    );
              }
                    return array(
                        'status'=>0,
                        'message'=>"录入失败"
                    );
        }
        else{//修改操作
            unset($data['date']);
             $b = D("test_data")->where('stu_id='.$stu_id.' and is_report='.$is_report.' and test_number='.$test_number.' and is_del=0 and year='.$year)->save($data);
                if($b !== false){
                    return array(
                        'status'=>1,
                        'message'=>'更新成功'
                    );
                }
                return array(
                    'status'=>0,
                    'message'=>'更新失败'
                );
        }
    }
    /*录入数据*/
    public function logging_data_all(){
        $data  = $_REQUEST['data'];
        $year  = $_REQUEST['year'];
        $datas = preg_split('/\//',$data);
        $is_report=0;
        $count = 0;
        for($i = 0;$i<count($datas);$i++){
            $where = preg_split('/,/',$datas[$i]);
            $test_id = $where[0];
            if($where[0]==-1){
                $project="modes_transportation";
            }else{
                $project=M('test_project')->where('project_id='.$where[0])->getField('english_name');
            }
            $stu_id = $where[1];
            $name   = M('student')->where('stu_id='.$where[1])->getField('stu_name');
            $score  = $where[2];
            $is_report    = $where[3];
            $this->data["is_report"]=$is_report;
            $test_number  = $where[4];
            $this->data["test_number"]=$test_number;
            $test = $this->logging_data($test_id,$project,$score,$stu_id,$name,$is_report,$test_number,$year);
            if($test['status']==1){
                $count++;
            }
        }
        $res=$this->is_test_data();
        if($count >= 1 ){
               return array(
                   'status'=>1,
                   'message'=>'保存成功'
               );
        }
        return array(
            'status'=>0,
            'message'=>'保存失败,请检查录入的数据是否正确'
        );
    }
    /*显示学生测试数据*/
    public function show_test_data()
    {
        $project_id   = $this->data['project'];//测试项目id
        $class_id     = $this->data['id'];//班级编号
        $is_report    = $this->data['is_report'];//是否上报
        $year         = $this->data['year'];//年份 2016
        $test_number  = $this->data['test_number'];//测试次数 
        if($project_id==-1){
            $english_name="modes_transportation";
        }else{
            $english_name    = M('test_project')->where('project_id='.$project_id)->getField('english_name');
            $english_name    = trim($english_name , '$'); 
        }
        $list = M('student')->field('stu_id,stu_name')->where('class_id='.$class_id.' and is_del=0')->findAll();
        $stu_id="";
        foreach($list as &$val) {
            $stu_id[]=$val['stu_id'];
            $map['stu_id']       = $val['stu_id'];
            $map['is_report']    = $is_report;
            $map['year']         = $year;
            $map['test_number']  = $test_number; 
            $map['is_del']       = 0;
            $test_data = M('test_data')->where($map)->field('test_id,stu_id,'.$english_name)->find();
            if($test_data) {
                $val['score'] = $test_data[$english_name];
            }else {
                $val['score'] = '';
            }
            $val['unit'] = M('test_project')->where('project_id='.$project_id)->getField('unit');
        }
        //返回数据
        if (!empty($list)) {
            return array(
                'status' => 1,
                'data' => $list,
                'message' => '成功返回数据'
            );
        }
        return array(
            'status' => 0,
            'data' => array(),
            'message' => '没有找到数据'
        );
    }
    /*已测数据*/
    public function is_test_data(){
        $map['class_id']    = $this->data['id'];//班级id
        $map['is_report']   = $this->data['is_report'];//是否上报1是 0否
        $map['test_number'] = $this->data['test_number'];//测试次数
        $map['year']        = $this->data['year'];//测试年份
        $map['is_del']      = 0;

        if($this->data["class_type"]) {
            $map['class_type'] = intval($this->data["class_type"]);
        } else {
            $user_group_id = M('user_group_link')->where('uid='.$_SESSION['mid'])->find();
            if( $user_group_id['user_group_id'] == 5 ) {
                $map['class_type'] = M('class')->where('class_id='.$map['class_id'])->getField('class_type');
            }
        };
	/*根据班级编号获取已测学生列表*/
        $student = D("test_data")->where($map)->select();
        /*获取当前学生是否完成测试项目*/
        for($i = 0;$i<count($student);$i++){
              $test_id = intval($student[$i]['test_id']);
              $bool = $this->is_test_over($test_id);
              $student[$i]['name'] = M('student')->where('stu_id='.$student[$i]['stu_id'])->getField('stu_name');
              $student[$i]['class_type'] = M('class')->where('class_id='.$map['class_id'])->getField('class_type');
              if($bool == 1){//完成测试
                    $student[$i]['is_complete_test'] = 1;
                    $maps['year']      = $student[$i]['year'];
                    $maps['test_type'] = $student[$i]['test_number'];
                    $maps['type']      = 5;
                    $maps['fid']       = $student[$i]['stu_id'];
                    $maps['is_report'] = $student[$i]['is_report'];
                    $maps['class_type']= $student[$i]['class_type'];

                    $heal_report = M('health_report')->where($maps)->find();
                    if( !$heal_report) {
                        D('HealthReport','public')->addHeathReport($student[$i] , $student[$i]['test_id']);//生成报告addHeathReport
                        //更新测试数据为待评价状态
                        $data['status'] = 0;
                        D("test_data")->where($map)->save($data);
                        
                    }
              }else{//未完成测试
                    $student[$i]['is_complete_test'] = 0;
              }
            $student[$i]['state'] = $student[$i]['status']==2 ? 1 : 0;
        }
        if(!empty($student)){
            return array(
                'status'=>1,
                'data'=>$student,
                'message'=>'成功返回已测学生数据'
            );
        }
        return array(
            'status'=>0,
            'data'=>array(),
            'message'=>'抱歉没有数据'
        );

    }
    /*判断某一学生是否完成测试*/
    public function is_test_over($test_id){
        /*查询字符串*/
        $where = "`balanceBeam` is not null and `continuousJump` is not null and `flexion` is not null and `standing_long_jump` is not null and
          `vision_right` is not null and `tennis_throw` is not null and `reentry_run` is not null and `vision_left` is not null and
          `skinfold_shoulder_armor` is not null and `skinfold_abdomen` is not null and `heart_rate` is not null and `skinfold_upper_arm` is not null and
          `weight` is not null and `sit_height` is not null and `modes_transportation` is not null and `height` is not null and test_id = {$test_id}";
        $student = D("test_data")->field('test_id')->where($where)->find();
        return $student ? 1 : 0;
    }
    /*判断某一测试数据是否已评价*/
    public function is_evaluate($test_id){
        $evaluate = D("test_data")->where(array('test_id'=>$test_id))->find();
        if( $evaluate['status']==2 ){
            return 1;
        }else{
            return 0;
        }
    }
    /*判断某学生是否已经推送报告*/
    public function is_push($stu_id){
        $data['stu_id'] = $stu_id;
        $arr = D("send_out")
            ->field("stu_id")
            ->where($data)
            ->find();
        if(!empty($arr)){
            return array(
                'status'=>1,
                'message'=>'此学生已发送报告'
            );
        }
        return array(
            'status'=>0,
            'message'=>'改学生未发送健康报告'
        );

    }
    /*园级账户下有哪些年级，有哪些班级*/
    public function get_grade_class(){
        $data['father_id'] = $_REQUEST['uid'];
        //获取年级
        $result = D("users")
                ->field("distinct grad")
                ->order("grad")
                ->where($data)
        ->select();
        //设置key
        $res = array();//初始化结果集
        for($i= 0;$i<count($result);$i++){
            $res[$result[$i]['grad']]=array();
        }
        //设置value
        foreach($res as $key => $value){
            $where['grad'] = $key;//grad
            $where['father_id'] = $_REQUEST['uid'];//uid
            $classes = D("users")
                    ->field("class")
                    ->order("class")
                    ->where($where)
                    ->select();
            $class = array_column($classes,"class");
            $res[$key] = $class;
        }
        $arr = array();//返回的结果集
        //初始化数据
        foreach($res as $key => $value){
            if($key == 1){
                $arr['small_class'] = array();
            }elseif($key == 2){
                $arr['middle_class'] = array();
            }elseif($key == 3){
                $arr['big_class'] = array();
            }
        }
       //遍历要返回的结果集，并赋值
        foreach($arr as $key=>$value){
            if($key == 'small_class'){
                $where['grad'] = 1;
                $where['father_id'] = $_REQUEST['uid'];
                $class = D('users')->field('class,class_id')->order("class")->where($where)->select();
                for($i = 0 ;$i<count($class);$i++){
                    $class[$i]['class'] = "小".$class[$i]['class']."班";
                }
                $arr[$key] = $class;
            }elseif($key == 'middle_class'){
                $where['grad'] = 2;
                $where['father_id'] = $_REQUEST['uid'];
                $class = D('users')->field('class,class_id')->order("class")->where($where)->select();
                for($i = 0 ;$i<count($class);$i++){
                    $class[$i]['class'] = "中".$class[$i]['class']."班";
                }
                $arr[$key] = $class;
            }elseif($key == 'big_class'){
                $where['grad'] =3;
                $where['father_id'] = $_REQUEST['uid'];
                $class = D('users')->field('class,class_id')->order("class")->where($where)->select();
                for($i = 0 ;$i<count($class);$i++){
                    $class[$i]['class'] = "大".$class[$i]['class']."班";
                }
                $arr[$key] = $class;
            }
        }
        //返回结果集
        if(!empty($arr)){
            return array(
                'status'=>1,
                'data'=>$arr,
                'message'=>'success'
            );
        }
            return array(
                'status'=>0,
                'data'=>array(),
                'message'=>'error'
            );

    }
    /*园级用户根据班级ID获取对应的学生列表*/
    public function get_student_by_class_id(){
        $data['class_id'] = $_REQUEST["class_id"];//班级ID
        $student_result = D("student")
                ->field("stu_id,stu_name")
                ->where($data)
                ->order("stu_id")
                ->select();
        if(!empty($student_result)){
            return array(
                'status'=>1,
                'data'=>$student_result,
                'message'=>'成功获取数据'
            );
        }
        return array(
            'status'=>0,
            'data'=>array(),
            'message'=>'对不起，暂时没有数据'
        );
    }

    //===========================图书管理接口=============================
    /**
     * 图书资讯列表
     *
     *图书资讯列表主要显示资讯的标题、时间、跟图片当用户想要了解详细的资讯、
     * 需要调用get_detailed_information接口
     * 详细请查看"详细资讯"部分的介绍跟代码
     */
    public function get_information_list()
    {
        $m = D("information");
        $arr = $m->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array(
                'status' => 0,
                'message' => "没有找到相关数据"
            );
        }
    }
    /**
     * 获取详细资讯
     *
     * 通过资讯id查询资讯的的信息
     * @param $ifm_id 资讯ID
     * @return false
     */
    public function get_detailed_information()
    {
        $ifm_id = $this->data['ifm_id'];
        $m = D("information");
        $arr = $m->where(array("ifm_id" => $ifm_id))->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array('status' => 0, 'message' => '没有相关数据');
        }
    }

    /**
     * 获取推荐图书列表
     *
     * 推荐列表主要显示推荐标题、发布时间以及图片、但我们要将所有的数据查询出来、方便在
     * 控制层与显示层的参数传递.
     */
    public function get_recommend_list()
    {
        $m = D("recommend_book");
        $arr = $m->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array('status' => 0, 'message' => '没有相关数据');
        }
    }
    /**
     * 获取详细图书内容、在详细页面中显示表标题、内容、图片
     *
     * @param $rcm_id 推荐ID
     * @return array or false
     */
    public function  get_detailed_recommend()
    {
        $rcm_id = $this->data['rcm_id'];
        $m = D("recommend_book");
        $arr = $m->where(array('rcm_id' => $rcm_id))->select();
        if (!empty($arr)) {
            return $arr;
        } else {
            return array('status' => 0, 'message' => '没有相关数据');
        }
    }
    /**
     * 获取图书分类
     *
     * @return array or false
     */
    public function get_book_type(){
        $data= M("book_type")->where('is_del=0')->select();
        return $data ?array('status' => 1,'result' => $data,'message' => '成功获取精选图书') : array('status' => 0,'message' => '获取数据失败');
         
    }
    public function get_book_publish(){
        $data= M("book_publish")->where('is_del=0')->select();
        return $data ?array('status' => 1,'result' => $data,'message' => '成功获取图书出版社') : array('status' => 0,'message' => '成功获取图书出版社');
         
    }
    public function _getBookCatalogById($id){
        $data=D("book_catalog")->where("id=".$id)->field("distinct title,info,cover,upload_server,publish,price,type,style,classify,bar_code")->find();
        $data['cover']=$this->_getAttachById($data["cover"],$data["upload_server"]);
        $data['publish']=$this->_getBookPublishById($data["publish"]);
        $data['type']=$this->_getBookTypeById($data["type"]);
        $data["style"]= $data["style"]==1 ? "精装" : "简装";
        $data["classify"]= $data["classify"]==1 ? "教师用书" : "幼儿用书";
        return $data;
    }
    public function _getBookTypeById($type_id){
        $data=D("book_type")->where("type_id=".$type_id)->find();
        return $data["name"];
    }
    public function _getBookPublishById($publish_id){
        $data=D("book_publish")->where("publish_id=".$publish_id)->find();
        return $data["publish_name"];
    }
    public function _getAttachById($attach_id,$server){
        if($server==1){
            $url="http://rhsd-lib.zhiyicx.com";
        }else{
            $url="http://xqtzjk.bjedu.cn/";
        }
        if($attachInfo = model('Attach')->where('attach_id='.$attach_id)->find()){
            return   $url."/data/upload/".$attachInfo['save_path'].$attachInfo['save_name'];
        }else{
            return  "http://rhsd-lib.zhiyicx.com/addons/theme/stv1/_static/image/hdfm.png";
        }
    }
    public function _getBookLocationById($location){
        $data=D("book_location")->where("location_id=".$location)->find();
        if($data["parent"]>0){
            $location=M("book_location")->where("location_id=".$data['parent'])->find();
            return $location['name']."/".$data["name"];
        }
        return $data["name"];
    }
    
    public function test(){
        $data=M("user")->where("fid=6824")->select();
        foreach($data as $key=>$value){
            M("user_group_link")->add(array("uid"=>$value['uid'],'user_group_id'=>5));
        }
        
    }
     
    /**
     * 获取图书列表
     *
     * @return array or false
     */
    public function get_book_list()
    {
        $type = $_REQUEST['book_type'];
        $max_id = $_REQUEST['max_id'] ? intval ($_REQUEST['max_id']) : 0;
        $count = $_REQUEST['count'] ? intval ($_REQUEST['count'] ) : 20;
        $uid=$_REQUEST['uid'];
        $fid=$_REQUEST['fid'];
        if($fid>0){
            $admin_id=array('0'=>$fid);
        }else{
            $admin_id=array('0'=>$uid);
        }
        $where=array(
            'admin_id'=>array('in',$admin_id),
        );
        $catalog_id = M('books')->where($where)->group('catalog_id')->select();
        $catalog_id = getSubByKey($catalog_id,'catalog_id');
        unset($where);
        $where=array(
            'type'=>$type,
            'id'=>array('in',$catalog_id),
        );
        $books = D("book_catalog")->where($where)->limit($max_id,$count)->select();
        if($books){
            for ($i = 0; $i < count($books); $i++) {
                $books[$i]["cover"]=$this->_getAttachById($books[$i]['cover'],$books[$i]["upload_server"]);
                $books[$i]["type"]=$this->_getBookTypeById($books[$i]["type"]);
                $arr=$this->_getTotalByCatalogId($books[$i]["id"],$admin_id);
                $books[$i]["number"]=$arr["count"];
                $books[$i]["normal"]=$arr["normal"];
                if($res=D("book_reserve_box")->where("del =0 and uid=".$uid." and catalog_id=".$books[$i]["id"])->find()){
                    $books[$i]["is_scheduled"]=1;
                    $books[$i]["reserve_id"]=$res;
                }else{
                    $books[$i]["is_scheduled"]=0;
                    $books[$i]["reserve_id"]=0;
                }
            }
        }
        if ($books) {
            return array(
                'status' => 1,
                'result' => $books,
                'message' => '获取数据成功'
            );
        } else {
            return array(
                'status' => 0,
                'message' => '该分类下暂无数据'
            );
        }
    }
    /**
     * 教师游客登录:
     *
     * 编程思路、游客登录主要就是当未注册用户点击登录时、会在数据库当中产生一个临时的账号和密码。
     * 游客不必输入用户名跟密码、即可登录进入系统。
     * 登录成功会存放一个session、
     * 当session不存在时、就可把相关游客数据进行清理
     * 1.添加临时账号跟密码
     */
    public function temporary_t_login()
    {
        //1.添加账号跟密码
        $m = D("temporary_tlogin");
        //用户名:
        $user = "temporary_user" . rand();
        //密码:
        $pwd = "temporary_pwd" . rand();
        $data = array('tmr_user' => $user, 'tmr_password' => $pwd);
        $b = $m->add($data);
        if ($b) {//登录成功
            //设置session
            session('username', $user);
            session('password', $pwd);
            return array('status' => 1, 'message' => '登录成功');
        } else {
            return array('status' => 0, 'message' => '登录失败');
        }
    }
    /**
     * 家长端游客登录
     * 家长游客登录与教师游客登录的原理基本一致、详情请查阅教师游客登录模块
     */
    public function temporary_f_login()
    {
        //1.添加账号跟密码
        $m = D("temporary_flogin");
        //用户名:
        $user = "temporary_user" . rand();
        //密码:
        $pwd = "temporary_pwd" . rand();
        $data = array('tpr_user' => $user, 'tpr_password' => $pwd);
        $b = $m->add($data);
        if ($b) {//登录成功
            //设置session
            session('username', $user);
            session('password', $pwd);
            return array('status' => 1, 'message' => '登录成功');
        } else {
            return array('status' => 0, 'message' => '登录失败');
        }
    }
    //2，发送通知
    public function push_notification(){
        $type = $this->data['type'];//1发送园级消息 2发送班级消息 3发送学生消息
        $receiver = $this->data['receiver'];
        if($type == 1) {
            $map['school_id'] = array('in' , $receiver);
            $family_id  = M('student')->where($map)->field('stu_id')->findAll();
            $family_id  = getSubByKey($family_id , 'stu_id');
            $family_ids = str_replace(',0', '', ','.implode(',', $family_id).',');
        } elseif($type == 2){
            $map['class_id'] = array('in' , $receiver);
            $family_id  = M('student')->where($map)->field('stu_id')->findAll();
            $family_id  = getSubByKey($family_id , 'stu_id');
            $family_ids = str_replace(',0', '', ','.implode(',', $family_id).',');
        } else {
            $family_ids = str_replace(',0', '', ','. $receiver.',');
            $family_ids  = explode(',',$receiver);
        }

        $data['uid']            = $this->data['uid'];
        $data['ntfaction_name'] = $_REQUEST['ntfaction_name'];
        $data['ntfaction_content'] = $_REQUEST['ntfaction_content'];
        $data['receiver'] = $family_ids;//接收者id
        $data['send_time'] = date('Y-m-d');
        $bool = D("notification")->add($data);
        if ($bool) {

            /*将对应的学生ID存储成功列表*/
            for ($i = 0; $i < count($family_id); $i++) {
                $push['stu_id'] = $family_id[$i];//id
                $push['send_time'] = date('Y-m-d');//time
                $push['ntfaction_id'] = $bool;
                D("push_notification")->add($push);
            }
            return array(
                'status' => 1,
                'id' => $bool,
                'message' => '发送成功'
            );
        }
        return array(
            'status' => 0,
            'message' => '发送失败'
        );
    }
    
    //家长端设置消息已收到 TODO
    public function setGet(){
        $id = $this->data['id'];
        $map['ntfaction_id'] = $id;
        $stu_ids=M('student')->where('family_id='.$this->mid)->field("stu_id")->select();
        $stu_ids=getSubByKey($stu_ids,"stu_id");
        $map['stu_id']       =array("in",$stu_ids) ;
        $res = M('push_notification')->where($map)->setField('is_read' , 1);
        return $res ? array('status'=>1) : array('status'=>0);
    }
    /*显示通知列表*/
    public function show_notification()
    {
        $max_id=$this->data["max_id"] ? $this->data["max_id"] : 0;
        $count=$this->data["count"] ? $this->data["count"] : 20;
        $data['uid'] = $this->data['uid'];
        $arr = D("notification")
            ->field("ntfaction_id,ntfaction_name,send_time")
            ->where($data)
            ->order("ntfaction_id desc")
            ->limit($max_id,$count)
            ->select();
        if (!empty($arr)) {
            return array(
                'status' => 1,
                'data' => $arr,
                'message' => '已加载完列表'
            );
        } else {
            return array(
                'status' => 0,
                'data' => array(),
                'message' => '加载列表失败'
            );
        }

    }
    /*查看通知详情*/
    public function get_detail_content()
    {
        $data['ntfaction_id'] = intval($_REQUEST['ntfaction_id']);

        $arr = M("notification")->field("uid,ntfaction_name,ntfaction_content,send_time")->where($data)->find();
        $arr['username'] = M('user')->where('uid='.$arr['uid'])->getField('uname');
        //获取已经收到消息的学生
        $data['is_read'] = 1;
        $stu_id = M('push_notification')->where($data)->field('stu_id')->select();
        $stu_id = getSubByKey($stu_id ,'stu_id');
        $is_read_user = M('student')->where( array('stu_id'=>array('in' , $stu_id)) )->limit('10')->findAll();
        
        unset($data['is_read']);
        $data['is_read'] = 0;
        $stu_id = M('push_notification')->where($data)->field('stu_id')->select();
        $stu_id = getSubByKey($stu_id ,'stu_id');
        
        $no_read_user = M('student')->where( array('stu_id'=>array('in' , $stu_id)) )->limit('10')->findAll();
        
        $arr['is_read_user'] = getSubByKey($is_read_user , 'stu_name');
        $arr['no_read_user'] = getSubByKey($no_read_user , 'stu_name');
        
        if (!empty($arr)) {
            return array(
                'status' => 1,
                'data' => $arr,
                'message' => '成功返回通知详情'
            );
        } else {
            return array(
                'status' => 0,
                'data' => array(),
                'message' => '没有数据'
            );
        }
    }
    /*删除通知*/
    public function delete_notification()
    {
        $data['ntfaction_id'] = $_REQUEST['ntfaction_id'];
        $bool = D('notification')->where($data)->delete();
        if ($bool >= 1) {
            return array(
                'status' => 1,
                'message' => '删除成功'
            );
        }
        return array(
            'status' => 0,
            'message' => '删除失败'
        );
    }
//4，获取收到报告的学生列表、
    //思路:判断收未收到报告的标准、在于学生是否有参加测试、可根据测试数据中的是否测试字段来进行判断
//6，获取轮播banner列表
    /**
     * 通过app传入不同的参数、获取不同的信息列表、主要包括banner图片路径、
     * 总共有4中不同情况1、探索、频道、读书、活动
     * Exploration:探索、  Channel：频道 、 read：读书 、Activity：活动
     * 默认值：Exploration
     */
    /**
     * @param string $type 参数：（Exploration:、  Channel： 、 read： 、Activity：）
     * @return string :返回对应类型的img路径
     */
    public function show_banner_list()
    {
        $type = $this->data['type'];
        $img = new Model("Carousel_img");
        //判断类型
        if (!empty($type) && isset($img)) {
            if (strtolower($type) == "exploration") {//探索类型
                //获取数据
                $exp_arr = $img->where(array('img_type' => 'exploration'))->select();
                return $exp_arr;
            } else if (strtolower($type) == "read") {//阅读类型
                //获取数据
                $read_arr = $img->where(array('img_type' => 'read'))->select();
                //转换json数据格式、并返回
                return $read_arr;
            } else if (strtolower($type) == "channel") {//频道类型
                //获取数据
                $channel_arr = $img->where(array('img_type' => 'channel'))->select();
                //转换json数据格式、并返回
                return $channel_arr;
            } else if (strtolower($type) == "activity") {//活动类型
                //获取数据
                $act_arr = $img->where(array('img_type' => 'activity'))->select();
                //转换json数据格式、并返回
                return $act_arr;
            } else {
                return array('status' => 0, 'message' => '获取图书信息失败');
            }
        }
    }
    //8，图书入库
    public function add_book()
    {
        $data["catalog_id"]=$this->data["catalog_id"];
        $data['ctime'] = time();
        $data['independent_code'] = $this->data['independent_code'];
        $data['admin_id'] = $this->data['uid'] ? $this->data['uid'] : $this->mid;
        $data['location'] = $this->data['location'];
        if($data["catalog_id"]==""){
           return array(
                'status' => 0,
                'message' => '请上传图书目录'
            ); 
        }
        if($data["independent_code"]==""){
           return array(
                'status' => 0,
                'message' => '请上传独立码'
            ); 
        }
        $result = D("books")->field('independent_code')->where(array('independent_code' => $data['independent_code']))->find();
        if (!empty($result)) {
            return array(
                'status' => 0,
                'message' => '入库失败、该图书已存在！'
            );
        } else {
            $bool = D("books")->add($data);
            switch ($bool) {
                case true:
                    return array(
                        'status' => 1,
                        'message' => '图书入库成功'
                    );
                    break;
                case false:
                    return array(
                        'status' => 0,
                        'message' => '图书入库失败'
                    );
                    break;
            }
        }
    }
    
    /*上传图片封面*/
    public function upload_cover() {
        $d ['attach_type'] = 'cover_image';
        $d ['upload_type'] = 'image';
        $GLOBALS ['fromMobile'] = true;
        $info = model ( 'Attach' )->upload ( $d, $d );
        if ($info ['status']) {
            $attach_id= getSubByKey ( $info ['info'], 'attach_id' );
            return $attach_id;
        } else {
            return array (
                    'status' => 0,
                    'msg' => '发布失败',
                    'data' =>null
            );
        }
    }
    //8，图书入库(同时添加图书目录)
    public function add_book_catalog()
    {
        $data["title"]=$this->data["title"];
        $data['cover'] =$this->data["cover"];
        $data['info'] = $this->data['info'];
        $data['publish'] = $this->data['publish'];
        $data['price'] = $this->data['price'];
        $data['type'] = $this->data['type'];
        $data['style'] = $this->data['style'];
        $data['classify'] = $this->data['classify'];
        $data['bar_code'] = $this->data['bar_code'];
        $data['upload_server'] = 2;
        $data['admin_id'] =$this->data['uid'] ? $this->data['uid'] : $this->mid;
        $data['ctime'] = time();
        if($data["title"]==""){
           return array(
                'status' => 0,
                'message' => '请上传图书目录名称'
            ); 
        }
        if($data["info"]==""){
           return array(
                'status' => 0,
                'message' => '请上传图书简介'
            ); 
        }
        if($data["bar_code"]==""){
           return array(
                'status' => 0,
                'message' => '请上传条形码'
            ); 
        }
        if($this->data["independent_code"]==""){
           return array(
                'status' => 0,
                'message' => '请上传独立码'
            ); 
        }
        if(D("books")->where("independent_code=".$this->data["independent_code"])->find()){
             return array(
                'status' => 0,
                'message' => '该独立码已存在'
            ); 
        }
        if(D("book_catalog")->where("bar_code=".$this->data["bar_code"])->find()){
             return array(
                'status' => 0,
                'message' => '该条形码已存在'
            ); 
        }
        $result = D("book_catalog")->field('bar_code')->where(array('bar_code' => $data['bar_code']))->find();
        if (!empty($result)) {
            return array(
                'status' => 0,
                'message' => '入库失败、该图书目录已存在！'
            );
        } else {
            $bool = D("book_catalog")->add($data);
            switch ($bool) {
                case true:
                    $where=array(
                        'catalog_id'=>$bool,
                        'independent_code'=>$this->data['independent_code'],
                        'admin_id'=>$data['admin_id'],
                        'ctime'=>time(),
                        'status'=>0,
                        'book_status'=>"正常",
                        'location_type'=>1,
                        'location'=>$this->data["location"],
                    );
                    D("books")->add($where);
                    return array(
                        'status' => 1,
                        'message' => '图书入库成功'
                    );
                    break;
                case false:
                    return array(
                        'status' => 0,
                        'message' => '图书入库失败'
                    );
                    break;
            }
        }
    }
    //图书移动
    public function move_book()
    {
        $book_id=$this->data["book_id"];
        $data['location'] = $this->data['location'];
        $data['admin_id'] =$this->data["uid"];
        $bool = D("books")->where("book_id=".$book_id)->save($data);
        switch ($bool) {
            case true:
                return array(
                    'status' => 1,
                    'message' => '图书移动成功'
                );
                break;
            case false:
                return array(
                    'status' => 0,
                    'message' => '图书移动失败'
                );
                break;
        }
    }
    /*通过图书目录查询相应的图书总数*/
    public function _getTotalByCatalogId($catalog_id,$admin_id){
        $where=array(
            'admin_id'=>array('in',$admin_id),
            'catalog_id'=>$catalog_id
        );
        $data=D("books")->where($where)->select();
        $normal=0;
        foreach ($data as $key => $value) {
            if($value["status"]==0){
                $normal++;
            }
        }
        return array("count"=>count($data),'normal'=>$normal);
    }
    /*通过图书目录查询相应的图书信息*/
    public function getTotalByCatalogInfo(){
        $catalog_id=$_REQUEST["catalog_id"];
        $admin_id=$_REQUEST["uid"];
        $where=array(
            'admin_id'=>array('in',$admin_id),
            'catalog_id'=>$catalog_id
        );
        $data=D("books")->where($where)->field("independent_code,status,location")->select();
        $normal=0;
        foreach ($data as $key => $value) {
            if($value["status"]==0){
                $data[$key]["info"]=$this->_getBookLocationById($value["location"]);
            }
            if($value["status"]==1){
                $data[$key]["info"]="借出";
            }
            if($value["status"]==2){
                $data[$key]["info"]="报损中";
            }
            if($value["status"]==3){
                $data[$key]["info"]="已报损";
            }
            if($value["status"]==4){
                $data[$key]["info"]="已预订";
            }
        }
        return $data ? $data : array();
    }
    /*显示预定列表*/
    public function show_reserve_list()
    {
        $uid = $_REQUEST['uid'] ? $_REQUEST['uid'] : $this->mid;
        $fid = $_REQUEST['fid'];
        $data=D("book_reserve_box")->where("del=0 and uid=".$uid)->select();
        if (!empty($data)) {
            foreach ($data as $key => $value) {
                $book_catalog=D("book_catalog")->where("id=".$value['catalog_id'])->find();
                $data[$key]["title"]=$book_catalog["title"];
                $data[$key]["price"]=$book_catalog["price"];
                $arr=$this->_getTotalByCatalogId($value['catalog_id'],$fid);
                $data[$key]["number"]=$arr["count"];
                $data[$key]["normal"]=$arr["normal"];
                $data[$key]["cover"]=$this->_getAttachById($value['cover'],$value["upload_server"]);
            }
            return array(
                'status' => 1,
                'result' => $data,
                'message' => '已查到相关数据'
            );
        }
        return array(
            'status' => 0,
            'message' => '没有相关数据'
        );
    }
    //添加预定箱
    public function add_reserve()
    {
        $data['uid'] = $_REQUEST['uid'] ? $_REQUEST['uid'] : $this->mid;
        $data['catalog_id'] = $_REQUEST['catalog_id'];
        $data['ctime']=time();
        $data['del']=0;
        $result = D("book_reserve_box")->add($data);
        if($result){
            return array(
                'status' => 1,
                'message' => '已添加到预定箱'
            );
        } else {
            return array(
                'status' => 0,
                'message' => '预定失败，请检查错误'
            );
        }
    }
    /*点击选中按钮操作*/
    public function click_button()
    {
        $uid = $_REQUEST['uid'];
        $reserve_id = $_REQUEST['reserve_id'];
        $data['uid'] = $uid;
        $data['reserve_id'] = $reserve_id;

        //获取当前按钮的状态值
        $result = D("reserve")->field("is_selected")->where($data)->select();
        $is_selected = $result[0]['is_selected'];
        if ($is_selected == "0") {//未被选中
            $bool = D("reserve")->where($data)->save(array('is_selected' => "1"));
            if ($bool) {
                $result = D("reserve")->field("is_selected")->where($data)->select();
                $is_selected = $result[0]['is_selected'];
                return array(
                    'status' => 1,
                    'message' => '已选中',
                    'now_is_selected' => $is_selected
                );
            }
        } else if ($is_selected == "1") {//选中状态
            $bool = D("reserve")->where($data)->save(array('is_selected' => "0"));
            if ($bool) {
                $result = D("reserve")->field("is_selected")->where($data)->select();
                $is_selected = $result[0]['is_selected'];
                return array(
                    'status' => 1,
                    'message' => '已取消',
                    'now_is_selected' => $is_selected
                );
            }
        } else {
            return array(
                'status' => 0,
                'message' => '不明状态，请联系管理员'
            );
        }
    }
    /*判断某一书籍是否已存在预定箱*/
    public function is_exist_reserve()
    {
        $data['book_id'] = $_REQUEST['book_id'];
        $data['uid'] = $_REQUEST['uid'];
        $arr = D("reserve")->where($data)->find();
        if ($arr) {
            return array(
                'is_reserve' => 1,
                'message' => '已存在预定箱'
            );
        }
        return array(
            'is_reserve' => 0,
            'message' => '不存在预定箱'
        );
    }
    /*取消预定(已预订成功图书)*/
    public function cancel_reserve(){
        $id=$_REQUEST["id"];
        $reserve_box_id=$_REQUEST["reserve_box_id"];
        $book_id=$_REQUEST["book_id"];
        $data['status']=1;
        $res=M("book_reserve")->where('id='.$id)->save($data);
        if ($res) {
            D("books")->where("book_id=".$book_id)->save(array('status'=>0));
            if(!M("book_reserve")->where('status=0 and reserve_box_id='.$reserve_box_id)->find()){
                if(M("book_reserve_box")->where('del>0 and id='.$reserve_box_id)){
                    M("book_reserve_box")->where('id='.$reserve_box_id)->save(array('del'=>-1));
                }
            }
            return array(
                'status' => 1,
                'message' => '取消预订成功'
            );
        }
        return array(
            'status' => 0,
            'message' => '取消预订失败，请检查网络'
        );
        
    }
    /*取消预定箱 图书目录*/
    public function delete_reserve()
    {
        $id = $_REQUEST['id'];
        if(is_numeric($id)){
            $map["id"]=$id;
        }else{
            $map["id"]=array("in",explode(",", $id));
        }
        $data['del']=-1;
        if(D("book_reserve_box")->where($map)->getField("del")==-1){
             return array(
                'status' => 0,
                'message' => '不能重复取消'
            );
        }
        if(D("book_reserve_box")->where($map)->getField("del")==1){
             return array(
                'status' => 0,
                'message' => '已预订成功，不能在预订箱取消'
            );
        }
        $bool = D("book_reserve_box")->where($map)->save($data);
        if ($bool) {
            return array(
                'status' => 1,
                'message' => '取消预订成功'
            );
        }
        return array(
            'status' => 0,
            'message' => '取消预订失败，请检查网络'
        );
    }
    /*删除预定箱 预定箱目录*/
    public function delete_reserve_two()
    {
        $data['book_id'] = $_REQUEST['book_id'];
        $data['uid'] = $_REQUEST['uid'] ? $_REQUEST['uid'] : $this->mid;
        $bool = D("reserve")->where($data)->delete();
        if ($bool) {
            return array(
                'status' => 1,
                'message' => '删除成功'
            );
        }
        return array(
            'status' => 0,
            'message' => '取消失败，请检查网络'
        );
    }
    /*确定预定*/
    /**
     * @param $data 预定书籍数据  array
     * @param $time 预定的时间  String
     * @param $handle_person 经手人 String
     * @param $borrow_person 借阅人 String
     */
    public function add_reserve_true()
    {
        $uid = $_REQUEST['uid'];
        $fid = $_REQUEST['fid'];
        $reserve=$_REQUEST['reserve'];//格式为：目录id/预订箱id/数量,目录id/预订箱id/数量
        $reserve_time = $_REQUEST['reserve_time'];
        $reserve_name = $_REQUEST['reserve_name'];
        $independent_code = $_REQUEST['independent_code'];
        $count=0;
        $num=0;
        $reserve_list=explode(",",$reserve);
        foreach ($reserve_list as $key => $value) {
            $arr=explode("/", $value);
            $num+=$arr[2];
            $reserve_box=D("book_reserve_box")->where("id=".$reserve_box_id)->find();
            $book=D("books")->where("status=0  and admin_id=".$fid." and catalog_id=".$arr[0])->field("book_id,independent_code")->limit($arr[2])->select();
            if(count($book)<$arr[2]){
                return array(
                    'status' => 0,
                    'message' => '图书库存不够'
                );
            }
            $reserve_box_id=$arr[1];
            foreach ($book as $k => $v) {
               $map=array( 
                    'uid'=>$uid,
                    'reserve_box_id'=>$reserve_box_id,
                    'book_id'=>$v['book_id'],
                    'independent_code'=>$v['independent_code'],
                    'ctime'=>time()
                );
                $res=D("book_reserve")->add($map);
                if($res){
                    D("books")->where("book_id=".$v["book_id"])->save(array("status"=>4,"book_status"=>"已预订"));
                    $data=array(
                        'reserve_name'=>$reserve_name,
                        'reserve_time'=>$reserve_time,
                        'del'=>1,
                    );
                    D("book_reserve_box")->where("id=".$reserve_box_id)->save($data);
                    $count++;
                }
            }
        }
        if ($count==$num) {
            return array(
                'status' => 1,
                'message' => '全部预定成功'
            );
        }else{
            return array(
                'status' => 0,
                'message' => '预定失败'
            );
        }
    }
    /*图书还回接口*/
    public function return_book()
    {
        $id = $_REQUEST['id'];
        $book_id=$_REQUEST['bid'];
        $data['endtime'] = date("Y-m-d H:i:s",time());
        $data['book_status'] = $_REQUEST['book_status'];
        $data['status'] = 2;
        //执行添加操作
        $bool = D("book_borrow")->where("id=".$id)->save($data);
        if ($bool > 0) {
            M("books")->where("book_id=".$book_id)->save(array('status'=>0,'book_status'=>'正常'));
            return array(
                'status' => 1,
                'message' => '还回成功'
            );
        } else {
            return array(
                'status' => 0,
                'message' => '图书还回失败'
            );
        }

    }
    /*#图书借阅#*/
    public function borrow_book()
    {
        /*准备数据*/
        $uid=$_REQUEST['uid'];
        if($uid){
            $data['uid'] =$uid;
        }
        $bid = $_REQUEST['bid'];
        $data['reserve_name']=$_REQUEST['reserve_name'];
        $data['handlers_name']=$_REQUEST['handlers_name'];
        $data['transactor_name']=$_REQUEST['transactor_name'];
        $data['status'] = 1;
        $data['starttime'] = date("Y-m-d H:i:s",time());
        $id=$_REQUEST['id'];//预定箱id
        $count=0;
        if(is_numeric($bid)){
            if(M("book_borrow")->where("status=1 and bid=".$bid)->find()){
                return array(
                    'status' => 0,
                    'message' => '该图书已被借阅'
                );
            }
            $book=M("books")->where("book_id=".$bid)->find();
            $data['bid']=$bid;
            $data["independent_code"]=$book["independent_code"];
            $bool=D("book_borrow")->add($data);
            if($bool){ 
                M("books")->where("book_id=".$data['bid'])->save(array("status"=>1,"book_status"=>"已借出"));
                M("book_reserve")->where("book_id=".$data['bid'])->save(array("status"=>2));
                $count++; 
            }
        }else{
            $bid=explode(",", $bid);
            foreach ($bid as $key => $value) {
                if(M("book_borrow")->where("status=1 and bid=".$value)->find()){
                    return array(
                        'status' => 0,
                        'message' => '该图书已被借阅'
                    );
                }
                $data['bid']=$value;
                $book=M("books")->where("book_id=".$value)->find();
                $data["independent_code"]=$book["independent_code"];
                $bool=D("book_borrow")->add($data);
                if($bool){
                    M("books")->where("book_id=".$data['bid'])->save(array("status"=>1,"book_status"=>"已借出"));
                    M("book_reserve")->where("book_id=".$data['bid'])->save(array("status"=>2));
                    $count++; 
                }
            }
        }
        //执行添加
        if ($count > 0) {
            //判断借阅图书是否通过预订，如果是预订，更改预订箱状态，判断预订图书是否全部借阅，没有释放该图书，回到正常状态
            $reserve=M("book_reserve")->where('reserve_box_id='.$id)->select();
            foreach ($reserve as $key => $value) {
                if($value['status']==0){
                    M("books")->where("book_id=".$value['book_id'])->save(array('status'=>0,'book_status'=>'正常'));
                    M("book_reserve")->where("book_id=".$value['book_id'])->save(array("status"=>1));
                }
            }
            D("book_reserve_box")->where('id='.$id)->save(array('signature'=>$_REQUEST['transactor_name'],'del'=>2)); 
            return array(
                'status' => 1,
                'message' => '图书借阅成功'
            );
        }
        return array(
            'status' => 0,
            'message' => '借阅失败'
        );
    }
    /*图书移动*/
    public function book_move()
    {
        $data['uid'] = $_REQUEST['uid'];
        $data['type'] = '图书';
        $arr = D("users")->field("admin_level")->where($data)->select();
        $level = $arr[0]['admin_level'];
        if ($level == 222) {//教师用户
            $area = array(
                '班级/办公室账号',
                '闲置'
            );
            return array(
                'status' => 1,
                'data' => $area,
                'message' => '成功返回教师用户图书移动位置'
            );
        } elseif ($level == 111) {//管理用户或园级用户
            $area = array(
                '图书馆',
                '资料室',
                '班级/办公室账号',
                '闲置'
            );
            return array(
                'status' => 1,
                'data' => $area,
                'message' => '成功返回园级用户图书移动位置'   //或者管理者
            );
        } else {
            return array(
                'status' => 0,
                'message' => '无效的用户信息'
            );
        }
    }
    /*搜索图书列表 模糊查询*/
    public function query_book_list()
    {
        $uid = intval($_REQUEST['uid']) ? intval($_REQUEST['uid']) : $this->mid;
        $fid = intval($_REQUEST['fid']);  
        if($fid>0){
            $admin_id=array("0"=>$fid);
        }else{
            $admin_id=array("0"=>$uid);
        }
        //获取用户输入的关键字
        $data['title'] = array('like', '%' . $_REQUEST['key'] . '%');
        $result = M("book_catalog")->where($data)->select();
        if($result){                                                                                        
            for ($i = 0; $i < count($result); $i++) {
                $result[$i]["cover"]=$this->_getAttachById($result[$i]['cover'],$result[$i]["upload_server"]);
                $result[$i]["type"]=$this->_getBookTypeById($result[$i]["type"]);
                $arr=$this->_getTotalByCatalogId($result[$i]["id"],$admin_id);
                $result[$i]["number"]=$arr["count"];
                $result[$i]["normal"]=$arr["normal"];
                $result[$i]['publish']=$this->_getBookPublishById($result[$i]["publish"]);
                $result[$i]['classify']=$result[$i]['classify']==1 ? "教师" : "幼儿";
                $result[$i]['style']=$result[$i]['style']==1 ? "精装" : "平装";
                if($res=D("book_reserve_box")->where("del >-1 and uid=".$uid." and catalog_id=".$result[$i]["id"])->find()){
                    $result[$i]["is_scheduled"]=1;
                    $result[$i]["reserve_id"]=$res['id'];
                }else{
                    $result[$i]["is_scheduled"]=0;
                    $result[$i]["reserve_id"]=0;
                }
            }
        }
        if (!empty($result)) {
            return array(
                'status' => 1,
                'data' => $result,
                'message' => '已获取数据'
            );
        }else{
           return array(
                'status' => 0, 
                'data' => array(),
                'message' => "没有找到相关书籍、换个关键字！"
            ); 
       }
    }
     /*获取图书类型*/
     public function getPublish(){
        $data=M("book_publish")->select();
        return $data ? $data : array();
     }
     /*获取图书类型*/
     public function getType(){
        $data=M("book_type")->select();
        return $data ? $data : array();
     }
     /*获取图书位置*/
    public function GetLocation()
    {
        $uid= $_REQUEST['uid'];
        $fid= $_REQUEST['fid'];
        if($fid>0){
            $where["admin_id"]=$uid;
        }else{
            $uids=M("user")->where("fid=".$uid)->field("uid")->select();
            $uids=getSubByKey($uids,"uid");
            $uids[]=$uid;
            $where['admin_id']=array('in',$uids);
        }
        $where['parent']=0;
        $data = D("book_location")->where($where)->field("location_id,name")->select();
        if ($data) {
             foreach ($data as $key => $value) {
                $res=D("book_location")->where('parent='.$value['location_id'])->field("location_id,name")->select();
                if($res){
                    $data[$key]['level']=$res;
                }
            }
            return array(
                'status' => 1,
                'data' => $data,
                'message' => 'true'
            );
        }
        return array(
            'status' => 0,
            'message' => '你没有移动图书的权限'
        );
    }
    /*更新图书位置location*/
    public function add_location()
    {
        $independent_code = $_REQUEST['independent_code'];
        $data['location'] = $_REQUEST['location'];
        $bool = D("books")
            ->where(array('independent_code' => $independent_code))
            ->save($data);
        if ($bool) {
            return array(
                'status' => 1,
                'message' => '操作已成功'
            );
        }
        return array(
            'status' => 0,
            'message' => '操作失败'
        );
    }
    /*扫描独立码或条形码返回相关的数据*/
    public function get_book_info()
    {   
        $code = $_REQUEST['code'];
        $data = D("books")
            ->where(array('independent_code' => $code))
            ->find();
        if($data){
            $data["location"]=$this->_getBookLocationById($data['location']);
            $data["catalog"]=$this->_getBookCatalogById($data['catalog_id']);
            if($data["status"]==1){
                $book_borrow = M("book_borrow")->where(array('independent_code' => $code,'status'=>1))->find();
                if ($book_borrow) {
                        $data["id"]=$book_borrow['id'];
                        $data["uid"]=$book_borrow['uid'];
                        $data["handlers_name"]=$book_borrow['handlers_name'];
                        $data["transactor_name"]=$book_borrow['transactor_name'];
                        $data["reserve_name"]=$book_borrow['reserve_name'];
                }
                if($data["status"]==4){
                    $book_reserve = M("book_reserve")->where(array('independent_code' => $code,'status'=>0))->find();
                    $data["reserve_box_id"]=$book_reserve['reserve_box_id'];
                }
            }
            $data["code_type"]=1;
            return array(
                'status' => 1,
                'data' => $data,
                'code' => $code,
                'message' => '成功返回图书信息'
            );
        }else{
             $data = M("book_catalog")
                ->field("distinct title,id,cover,publish,price,info,upload_server,type,style,classify,bar_code")
                ->where(array('bar_code' => $code))
                ->find();
            if (!empty($data)) {
                $data['cover']=$this->_getAttachById($data["cover"],$data["upload_server"]);
                $data['publish']=$this->_getBookPublishById($data["publish"]);
                $data['type']=$this->_getBookTypeById($data["type"]);
                $data["style"]= $data["style"]==1 ? "精装" : "简装";
                $data["classify"]= $data["classify"]==1 ? "教师用书" : "幼儿用书";
                $data["code_type"]=2;
                return array(
                    'status' => 1,
                    'data' => $data,
                    'code' => $code,
                    'message' => '成功返回条形码数据'
                );
            }else{
                return array(
                    'status' => 0,
                    'data' => "",
                    'code' => $code,
                    'message' => '返回数据失败'
                );
            }
        }
    }
    /*已（被）预订列表*/
    public function reserve_history(){
        $reserve_name=$_REQUEST["reserve_name"];
        $reserve_box_id=$_REQUEST["reserve_box_id"];
        if($reserve_box_id){
            $where["id"]=$reserve_box_id;
        }else{
           $uid=$_REQUEST['uid'] ? $_REQUEST['uid'] : $this->mid;
            $userGroup=M("user_group_link")->where("uid=".$uid)->find();
            if($userGroup['user_group_id']==4){
                $books=D("books")->field("book_id")->where('admin_id='.$uid)->select();
                $book_ids=getSubByKey($books,"book_id");


                $where['book_id']=array('in',$book_ids);
                $where['status']=0;
                $book_reserve=D("book_reserve")->group("reserve_box_id")->field("reserve_box_id")->where($where)->select();
                $reserve_box_id=getSubByKey($book_reserve,"reserve_box_id");
                unset($where);
                $where['id']=array('in',$reserve_box_id);
            }else{
                $where["uid"]=$uid;
            } 
        }
        $max_id = $_REQUEST['max_id'] ? intval ($_REQUEST['max_id']) : 0;
        $count = $_REQUEST['count'] ? intval ($_REQUEST['count'] ) : 20;
        $where["del"]=array("in",array(1,3));
        if($reserve_name){
            $where["reserve_name"]=array('like' , '%'.$reserve_name.'%');
        }
        $data = D("book_reserve_box")->where($where)->order("ctime desc")->limit($max_id,$count)->select();
        foreach ($data as $key => $value) {
            $reserve="";
            $book_catalog=D("book_catalog")->where("id=".$value["catalog_id"])->find();
            $reserve=D("book_reserve")->field("book_id,independent_code,id")->where("status=0 and reserve_box_id=".$value["id"])->select();
            $data[$key]["book"]=$reserve;
            $data[$key]["school_name"]=$this->_getBookAdminById($reserve[0]["book_id"]);
            $data[$key]["number"]=count($reserve);
            $data[$key]["cover"]=$this->_getAttachById($book_catalog['cover'],$book_catalog["upload_server"]);
            $data[$key]["title"]=$book_catalog['title'];
            $data[$key]["type"]=$this->_getBookTypeById($book_catalog["type"]);
            $data[$key]["is_failure"]=$value["del"]==3 ?  1 : 0 ;
        }
        if($data){
            return array(
                'status'=>1,
                'data'=>$data,
                'message'=>'成功返回历史数据'
            );
        }
        return array(
            'status'=>0,
            'data'=>array(),
            'message'=>'没有历史数据'
        );
    }
    public function _getBookAdminById($book_id){
        $book=M("books")->where("book_id=".$book_id)->find();
        $user=model("User")->where("uid=".$book["admin_id"])->find();
        return $user["uname"];
    }
    /*借阅历史列表*/
    public function borrow_history(){
        $uid= $_REQUEST['uid'] ? $_REQUEST['uid'] : $this->mid;
        $fid= $_REQUEST['fid'];
        $reserve_name= $_REQUEST['reserve_name'];
        $max_id = $_REQUEST['max_id'] ? intval ($_REQUEST['max_id']) : 0;
        $count = $_REQUEST['count'] ? intval ($_REQUEST['count'] ) : 20;
        if($fid>0){
            $where["uid"]=$uid;
        }else{
            $books=D("books")->field("book_id")->where('admin_id='.$uid)->select();
            $book_ids=getSubByKey($books,"book_id");
            $where['bid']=array('in',$book_ids);
        }
        if($reserve_name){                   
            $where['reserve_name']= array('like','%'.$reserve_name.'%');
        }
        $result = D("book_borrow")->order("id desc")->limit($max_id,$count)->group("bid")->where($where)->select();
        foreach ($result as $key => $value) {
            $book=D("books")->where("book_id=".$value['bid'])->find();
            $result[$key]['borrow_type']= $value['uid'] ? "预订借阅" : "直接借阅";
            if($value['status']==1){
                $result[$key]['status']="已借出";
            }else{
                $result[$key]['status']="已还回";
            }
            $result[$key]["catalog"]=$this->_getBookCatalogById($book['catalog_id']);
        }
        if(!empty($result)){
            return array(
                'status'=>1,
                'data'=>$result,
                'message'=>'成功返回历史数据'
            );
        }
        return array(
            'status'=>0,
            'data'=>array(),
            'message'=>'当前用户没有借阅书籍'
        );
    }
    
    
    
    
    /*+++++++++++++++++家长端补充接口+++++++++++++++++++++++++*/
    
    /*
     * 获取通知列表
     */
    function getNotifyList(){
        $max_id=$this->data["max_id"] ? $this->data["max_id"] : 0;
        $count=$this->data["count"] ? $this->data["count"] : 20;
        $stu_ids=M('student')->where('family_id='.$this->mid)->field('stu_id')->findAll();
        foreach ($stu_ids as $key => $value) {
            if($key+1==count($stu_ids)){
                $stu_id.=$value['stu_id'];
            }else{
                $stu_id.=$value['stu_id'].",";
            }
        }
        if(!$stu_ids){
            return array();
        }
        $map['receiver'] = array('like','%'.$stu_id.'%');
        $list =  M('notification')->where($map)->order("ntfaction_id desc")->limit($max_id,$count)->findAll();
        foreach ($list as $key => $value) {
            $stu_name="";
            foreach ($stu_ids as $k=> $v) {
                if(strstr($value["receiver"],$v["stu_id"])){
                    $student=D("student")->where("stu_id=".$v["stu_id"])->find();
                    $stu_name.="  ".$student["stu_name"];
                }
            }
            $list[$key]["stu_name"]=$stu_name;
        }
        return $list ? $list : array();
    }
    
    /*
     * 获取通知详情
     */
    public function getNotifyOne(){
        $map['ntfaction_id'] = $this->data['id'];
        return M('notification')->where($map)->find();
    }
    
    /*
     * 获取体质报告列表
     */
    public function getReportList(){
        //TODO 表缺少字段
        $stu_ids=M('student')->where('family_id='.$this->mid)->field('stu_id')->findAll();
        $stu_ids=getSubByKey($stu_ids,"stu_id");
        $map['fid']= array("in",$stu_ids);
        $map['is_report'] = 1;
        $map['type']      = 5;
        $map['status']    = 2;
        $list = M('health_report')->where($map)->field('rpt_id,rpt_name,detailed_info')->limit($this->_limit())->findAll();
        foreach($list as &$val){
            $val['file_path'] = SITE_URL.'/index.php?app=public&mod=Report&act=Report&rpt_id='.$val['rpt_id'];
        }
        return $list ? $list : array();
    }
    
    //获取园数据
    function getSchool(){
        $fid = $this->data['fid'];//区id
        $list = M('school')->where( 'area='.$fid )->findAll();
        return $list ? $list : array();
    }
    
    //获取班级数据
    function getClass(){
        $map['school_id'] = $this->data['school_id'];
        $map['is_del']    = 0;
        $list = M('class')->where($map)->findAll();
        return $list ? $list : array();
    }
    /*
     * 获取绑定的学生信息
     */
    public function getStudentByUid(){
        $result=M("student")->where("family_id=".$this->mid)->field("stu_name,birthday,class_id,school_id,stu_id")->select();
        foreach ($result as $key => $value) {
            $school=M("school")->where("id=".$value["school_id"])->find();
            $class=M("class")->where("class_id=".$value["class_id"])->find();
            $result[$key]["class_name"]=$class["class_name"];
            $result[$key]["school_name"]=$school["name"];
        }
        return $result ? $result : array();
    }
    /*
     * 验证学生信息
     */
    public function checkStudent(){
        $result=M("student")->where("family_id=".$this->mid)->select();
        if(count($result)>3){
            return array('status'=>'0','message'=>"一个家长最多只能绑定三个孩子");
        }
        $data['family_id']         =0;
        $data['gender']            = $this->data['gender'];          //性别 1男 2女
        $data['birthday']          = date('Y-m-d' , strtotime($this->data['birthday']) );        //生日  时间戳
        $data['school_id']         = $this->data['school_id'];       //学校id
        $data['class_id']          = $this->data['class_id'];        //班级id
        $data['stu_name']          = t($this->data['stu_name']);        //学生姓名
        $data['is_del']            = 0;
        $res = M('student')->where($data)->find();
        if ($res) {
            $map['family_id'] = $this->mid;
            M('student')->where('stu_id='.$res['stu_id'])->save($map);
            return array('status'=>'1','message'=>"绑定成功！");
        } else {
            return array('status'=>'0','message'=>"没有找到这个学生或者已被绑定");

        }
    }
    /*
     * 取消绑定学生
     */

    public function unbundlingStudent(){
        if (M("student")->where("stu_id=".$this->data["stu_id"])->save(array("family_id"=>0))) {
            return array('status'=>'1','message'=>"解绑成功！");
        } else {
            return array('status'=>'0','message'=>"没有找到这个学生");
        }
    }
    /*
     * 我的应用列表-文章列表
     */
    public function getArticleList(){
        $list = M('x_article')->limit($this->_limit())->order('sort desc')->findAll();
        foreach($list as & $val){
            $attachInfo = model('Attach')->where('attach_id='.$val['attach'])->find();
            $val['icon'] = UPLOAD_URL.'/'.$attachInfo['save_path'].$attachInfo['save_name'];
        }
        return $list ? $list : array();
    }
    
    /*
     * 获取文章详情
     */
    public function getArticleOne(){
        $map['id'] = $data['id'];
        $res = M('x_article')->where($map)->find();
        $res['icon'] = getImageUrlByAttachId($value['attach']);
        return $res;
    } 
    /*
     * 获取推荐图书列表
     */
    public function getRecommendBookList(){
        $max_id = $_REQUEST['max_id'] ? intval ($_REQUEST['max_id']) : 0;
        $count = $_REQUEST['count'] ? intval ($_REQUEST['count'] ) : 20;
        $list = M('book_catalog')->where('is_recommend = 1')->field("id,title,cover,upload_server,info,publish,price,type,style")->limit($max_id,$count)->findAll();
        foreach ($list as $key => $value) {
            $list[$key]["type"]=$this->_getBookTypeById($value["type"]);
            $list[$key]["cover"]=$this->_getAttachById($value['cover'],$value["upload_server"]);
            $list[$key]["publish"]=$this->_getBookPublishById($value['publish']);
            $list[$key]["style"]=$value['style']==1 ? "精装" : "平装";
        }
        
        return $list ? $list : array();
    }
    /*
     * 获取推荐图书评论列表
     */
    public function getRecommendBookCommentList(){
        $catalog_id=$_REQUEST["id"];
        $max_id = $_REQUEST['max_id'] ? intval ($_REQUEST['max_id']) : 0;
        $count = $_REQUEST['count'] ? intval ($_REQUEST['count'] ) : 20;
        $data = D("book_catalog_comment")->where("catalog_id=".$catalog_id)->limit($max_id,$count)->select();
        foreach ($data as $key => $value) {
            $user=model("User")->where("uid=".$value['uid'])->find();
            $data[$key]["uname"]=$user["uname"];
            $data[$key]["avatar"]=getUserFace($value['uid']);
            $data[$key]["time"]=date("Y-m-d H:i:s",$value['uid']);
        }
        return $data ? $data : array();
    }
    /*
     * 添加推荐图书评论
     */
    public function addRecommendBookComment(){
        $catalog_id=$_REQUEST["id"];
        $uid = $this->mid;
        $comment = $_REQUEST['comment'];
        $data=array(
            'catalog_id'=>$catalog_id,
            'uid'=>$uid,
            'comment'=>$comment,
            'ctime'=>time()
        );
        $res = D("book_catalog_comment")->add($data);
        return $res ? array('status'=>'1','message'=>true) :array('status'=>'0','message'=>false);
    }
    
    /* 
     * 验证当前用户是否绑定学生
     */
    function is_bundling(){
        $map['family_id'] = $this->mid;
        $res = M('student')->where($map)->field("stu_id")->select();
        return $res ?  array($res)  : array();
    }
    

    //获取测试项的英文和中文名
    function getTestName(){
        $list = M('test_project')->getField('english_name,project_name');
        return $list ? $list : array();
    }
    public function getCoverById($id){
        $data=D("book_catalog")->where("id=".$id)->field("cover")->find();
        return $data["cover"];
    }
    //获取用户添加的图书管理系统幼儿园
    function getUserLibraryGraden(){
        $data=M("user_school")->where('uid='.$this->mid)->field("school_id")->find();
        if($data){
            $user=model("User")->where("uid=".$data['school_id'])->field("area,uname")->find();
            $area=model("Area")->getAreaById($user["area"]);
            $data["area_name"]=$area["title"];
            $data["area_id"]=$user["area"];
            $data["school_name"]=$user["uname"];
        }
        return $data ? $data : array();
    }
    //获取图书管理系统幼儿园
    function getLibraryGraden(){
        $area=$_REQUEST["area"];
        $where=array(
            'permissions'=>2,
            'fid'=>0,
            'is_del'=>0,
            'area'=>$area,
        );
        $data=model("User")->where($where)->field("uname,uid")->select();
        return $data;
    }
    //选择图书管理系统幼儿园
    function selectLibraryGraden(){
        $school_id=$_REQUEST["school_id"];
        $res=M("user_school")->where("uid=".$this->mid)->find();
        if($res){
            if(M("user_school")->where("id=".$res['id'])->save(array('school_id'=>$school_id))){
                return array('status'=>'1','message'=>true);
            }else{
                return array('status'=>'0','message'=>false);
            }
        }else{
            $data=array(
                'uid'=>$this->mid,
                'school_id'=>$school_id
            );
            if(M("user_school")->add($data)){
                return array('status'=>'1','message'=>true);
            }else{
                return array('status'=>'0','message'=>false);
            }
        }
    }
}
